﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

using DesertedRoadStudios.BlogNetNuke.Common;
using DesertedRoadStudios.BlogNetNuke.Components;

namespace DesertedRoadStudios.BlogNetNuke
{
    public class BNNDefault : DotNetNuke.Framework.CDefault
    {
        private BlogTab _blog;

        protected void Page_Init(object sender, EventArgs e)
        {
            string initRequest = Request.RawUrl;
            string initDomain = Request.Url.Host;
            string newRequest = string.Empty;

            // First check if this is a TinyURL request
            BlogNetNukeController bnn = new BlogNetNukeController();
            List<string> domains = bnn.GetTinyUrlDomains();
            bool domainFound = false;
            string shortURL = string.Empty;
            foreach (string domain in domains)
            {
                if (domain.Equals(initDomain, StringComparison.InvariantCultureIgnoreCase))
                {
                    shortURL = domain;
                    domainFound = true;
                    break;
                }
            }

            if (domainFound)
            {
                Regex codeREX = new Regex("^https?://" + shortURL + "/(.+){8,8}");
                if (codeREX.IsMatch(initRequest))
                {
                    throw new SystemException(codeREX.Replace(initRequest, "$0"));
                }
            }

            Common.ParseData data;

            //Regex sitemapREX = new Regex("(.+)/sitemap\\.aspx", RegexOptions.IgnoreCase);
            //Regex categoryREX = new Regex("(.+)/Category/(.+)\\.aspx", RegexOptions.IgnoreCase);
            //Regex tagREX = new Regex("(.+)/Tag/(.+)\\.aspx", RegexOptions.IgnoreCase);
            //Regex slugREX = new Regex("(.+)/(\\d+)/(\\d+)/(\\d+)/(.+)\\.aspx", RegexOptions.IgnoreCase);
            //Regex dayREX = new Regex("(.+)/(\\d+)/(\\d+)/(\\d+)\\.aspx", RegexOptions.IgnoreCase);
            //Regex monthREX = new Regex("(.+)/(\\d+)/(\\d+)\\.aspx", RegexOptions.IgnoreCase);
            //Regex yearREX = new Regex("(.+)/(\\d+)\\.aspx", RegexOptions.IgnoreCase);

            Regex sitemapREX = new Regex("(.+)/sitemap(.+)", RegexOptions.IgnoreCase);
            Regex categoryREX = new Regex("(.+)/Category/(.+)", RegexOptions.IgnoreCase);
            Regex tagREX = new Regex("(.+)/Tag/(.+)", RegexOptions.IgnoreCase);
            Regex slugREX = new Regex("(.+)/(\\d+)/(\\d+)/(\\d+)/(.+)", RegexOptions.IgnoreCase);
            Regex dayREX = new Regex("(.+)/(\\d+)/(\\d+)/(\\d+)(.*)", RegexOptions.IgnoreCase);
            Regex monthREX = new Regex("(.+)/(\\d+)/(\\d+)(.*)", RegexOptions.IgnoreCase);
            Regex yearREX = new Regex("(.+)/(\\d+)(.*)", RegexOptions.IgnoreCase);

            if (sitemapREX.IsMatch(initRequest))
            {
                newRequest = sitemapREX.Replace(initRequest, "~/Default.aspx?TabID=[!TabID!]&Sitemap=1&Parsed=1");
                GetTabID(sitemapREX.Replace(initRequest, "$1"));
            }
            else if (categoryREX.IsMatch(initRequest))
            {
                GetTabID(categoryREX.Replace(initRequest, "$1"));
                data = Methods.Parse(categoryREX.Replace(initRequest, "$2"), _blog);

                newRequest = categoryREX.Replace(initRequest, "~/Default.aspx?TabID=[!TabID!]&Category=" + data.path + "&Parsed=1" + data.query);
            }
            else if (tagREX.IsMatch(initRequest))
            {
                GetTabID(tagREX.Replace(initRequest, "$1"));
                data = Methods.Parse(tagREX.Replace(initRequest, "$2"), _blog);

                newRequest = tagREX.Replace(initRequest, "~/Default.aspx?TabID=[!TabID!]&Tag=" + data.path + "&Parsed=1" + data.query);
            }
            else if (slugREX.IsMatch(initRequest))
            {
                GetTabID(slugREX.Replace(initRequest, "$1"));
                data = Methods.Parse(slugREX.Replace(initRequest, "$5"), _blog);

                newRequest = slugREX.Replace(initRequest, "~/Default.aspx?TabID=[!TabID!]&Year=$2&Month=$3&Day=$4&Slug=" + data.path + "&Parsed=1" + data.query);
            }
            else if (dayREX.IsMatch(initRequest))
            {
                GetTabID(dayREX.Replace(initRequest, "$1"));
                data = Methods.Parse(dayREX.Replace(initRequest, "$5"), false, _blog);

                newRequest = dayREX.Replace(initRequest, "~/Default.aspx?TabID=[!TabID!]&Year=$2&Month=$3&Day=$4&Parsed=1" + data.query);
            }
            else if (monthREX.IsMatch(initRequest))
            {
                GetTabID(monthREX.Replace(initRequest, "$1"));
                data = Methods.Parse(monthREX.Replace(initRequest, "$4"), false, _blog);

                newRequest = monthREX.Replace(initRequest, "~/Default.aspx?TabID=[!TabID!]&Year=$2&Month=$3&Parsed=1" + data.query);
            }
            else if (yearREX.IsMatch(initRequest))
            {
                GetTabID(yearREX.Replace(initRequest, "$1"));
                data = Methods.Parse(yearREX.Replace(initRequest, "$3"), false, _blog);

                newRequest = yearREX.Replace(initRequest, "~/Default.aspx?TabID=[!TabID!]&Year=$2&Parsed=1" + data.query);
            }
            else // Full View
            {
                newRequest = initRequest;

                // Remove page extension if exists
                if (newRequest.Contains("."))
                    newRequest = newRequest.Substring(0, newRequest.IndexOf("."));

                GetTabID(newRequest);
                data = Methods.Parse(initRequest, true, _blog);
                
                newRequest = "~/Default.aspx?TabID=[!TabID!]&Parsed=1" + data.query;
            }

            //DotNetNuke.Entities.Tabs.TabController tcon = new DotNetNuke.Entities.Tabs.TabController();
            //DotNetNuke.Entities.Tabs.TabInfo tinfo = tcon.GetTab(66, PortalSettings.PortalId, true);

            Server.TransferRequest(newRequest.Replace("[!TabID!]", _blog.TabID.ToString()), true);

        }

        private void GetTabID(string tabPath)
        {
            tabPath = tabPath.Replace("/", "//");
            
            BlogNetNukeController controller = new BlogNetNukeController();
            _blog = controller.GetBlogTabInfo(PortalSettings.PortalId, tabPath);
        }

    }
}
